{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#梯度消失\n",
    "%matplotlib inline\n",
    "import torch\n",
    "from d2l import torch as d2l\n",
    "\n",
    "x = torch.range(-8.0,8.0,0.1,requires_grad=True)\n",
    "y = torch.sigmoid(x)\n",
    "\n",
    "y.backward(torch.ones_like(x))\n",
    "\n",
    "d2l.plot(x.detach().numpy(),[y.detach().numpy(),x.grad.numpy()],legend=['sigmoid','gradient'],figsize=(4.5,2.5))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#梯度爆炸\n",
    "M = torch.normal(0,1,size=(4,4))\n",
    "print(\"一个矩阵 \\n\",M)\n",
    "\n",
    "for i in range(100):\n",
    "    M = torch.mm(M,torch.normal(0,1,size=(4,4)))\n",
    "print(\"乘以100个矩阵后\\n\",M)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "82ec87b6",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一个矩阵 \n",
      " tensor([[ 0.1922, -0.6707, -1.0487,  0.2323],\n",
      "        [-0.1405,  0.1740, -0.6231,  1.6164],\n",
      "        [ 0.4084,  0.2760, -0.9020, -0.7431],\n",
      "        [ 1.9831, -0.5449,  0.9569, -0.8187]])\n",
      "乘以100个矩阵后\n",
      " tensor([[-8.1230e+24, -1.5127e+25, -2.3717e+24, -1.5805e+24],\n",
      "        [-4.2443e+25, -7.9042e+25, -1.2392e+25, -8.2583e+24],\n",
      "        [-7.3614e+25, -1.3709e+26, -2.1493e+25, -1.4323e+25],\n",
      "        [ 1.6991e+26,  3.1642e+26,  4.9609e+25,  3.3060e+25]])\n"
     ]
    }
   ],
   "source": [
    "#梯度爆炸\n",
    "M = torch.normal(0,1,size=(4,4))\n",
    "print(\"一个矩阵 \\n\",M)\n",
    "\n",
    "for i in range(100):\n",
    "    M = torch.mm(M,torch.normal(0,1,size=(4,4)))\n",
    "print(\"乘以100个矩阵后\\n\",M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61083069-c906-4a38-9aac-9d1de8856655",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}